//
//  MyRecommendVC.m
//  PlayCatchGame
//
//  Created by 流言 on 2018/3/2.
//  Copyright © 2018年 清正. All rights reserved.
//

#import "MyRecommendVC.h"
#import "AppHeader.h"
#import "MyRecommendCell.h"

#import "UserModel.h"

@interface MyRecommendVC () {
    
    NSInteger currentPage;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSMutableArray<UserModel *> *dataArray;

@end

@implementation MyRecommendVC

- (void)viewDidLoad {
    [super viewDidLoad];
    currentPage = 1;
    self.dataArray = [NSMutableArray array];
    
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        currentPage = 1;
        [self.dataArray removeAllObjects];
        [self _requestNetwork];
        [self.tableView.mj_footer setState:MJRefreshStateIdle];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackStateFooter footerWithRefreshingBlock:^{
        currentPage++;
        [self _requestNetwork];
    }];
    
    [self.tableView.mj_header beginRefreshing];
    
}

#pragma mark - Request Network
- (void)_requestNetwork {
    [HttpUrl NetErrorGET:@"user/selectMyInviList" dict:@{@"current":@(currentPage)} hud:self.view isShow:NO WithSuccessBlock:^(id data) {
        if (BB_isSuccess) {
            NSArray *list = data[@"data"][@"list"];
            if (list.count == 0) {
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            } else {
                for (NSDictionary *dic in list) {
                    UserModel *model = [UserModel mj_objectWithKeyValues:dic];
                    [self.dataArray addObject:model];
                }
                [self.tableView reloadData];
                [self.tableView.mj_footer endRefreshing];
            }
            [self.tableView.mj_header endRefreshing];
        } else {
            ShowAlertTitle(data[@"msg"]);
        }

    }WithFailBlock:^(id data) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        ShowAlertTitle(@"网络连接错误");
    }];
    
    
    
    
}

#pragma mark - UITableViewDelegate & UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 70;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.1;
}

- (UIView*)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    UIView *headerView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(tableView.frame), 0.1)];
    headerView.backgroundColor =tableView.backgroundColor;
    return headerView;
}

- (UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footerView =[[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 0.1)];
    footerView.backgroundColor =tableView.backgroundColor;
    
    return footerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyRecommendCell *cell =[tableView dequeueReusableCellWithIdentifier:@"MyRecommendCell"];
    if (!cell) {
        cell =[MyRecommendCell findCellWithIdentifier:@"MyRecommendCell"];
        cell.selectionStyle =UITableViewCellSelectionStyleNone;
    }
    UserModel *model = self.dataArray[indexPath.row];
    cell.nameLabel.text = [NSString stringWithFormat:@"%@", model.userName];
    [cell.headImageView sd_setImageWithURL:[NSURL URLWithString:model.headImg] placeholderImage:[UIImage imageNamed:@"touxiang"]];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"点击cell");
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
